***********************************************************************
*                        PREPARE DATA FOR REGRESSIONS                 *
***********************************************************************
* All regressions with non-characteristic outcomes are run *with* patient FEs and saved in a folder called reg_trad_patientFEs
* All regressions with characteristic outcomes are run without patient characteristics and saved in a folder called reg_trad_chars
 
*--------------------------------------------------
log using "${SIDCodePath}/sidsedd_dataprep.log", replace
use "${SIDDataPath}/sidsedd_merged_20102016.dta", clear

*--------------------------------------------------
* Define variables
*--------------------------------------------------
	// Create subsamples depending on how many hours away from midnight they were
	forval i = 1/5{
		gen 	hourstilMN`i' 	  		= 0
		replace hourstilMN`i' 			= 1 ///
										if edhour_2 >= (-100)*`i' & edhour_2 <= 100*`i' - 100
	}

	// Generate post-policy dummy
	gen 	dqtr_2 						= (dqtr - 1)/4
	gen 	year_dqtr 					= year + dqtr_2 /* This value is defined s.t. 2012.0 = first quarter of 2012, 2012.25 = second quarter of 2012, etc... */
	gen 	post_qtr 					= 0
	replace post_qtr 					= 1 if year_dqtr >= 2013.75

	// After-midnight dummy
	gen 	after_MN 					= 0
	replace after_MN 					= 1 ///
										if edhour_2 >= 0 
	// 11pm dummy
	gen edhour_11pm = edhour_2 == -100

	gen 	post_qtr_after_MN 			= post_qtr * after_MN

	// Observation dummy (since hcup_os == 3 when observation status)
	gen 	obs 						= cond(hcup_os > 0, 1, 0)
	gen 	out 					    = cond(!obs & !inpatient, 1, 0)

	// Generate dummy which is 1 if in medicare, and 0 if not medicare and age <= 50 (to reduce spillovers)
	gen 	medicare50 					= .
	replace medicare50 					= 0 ///
										if age <= 50 & medicare == 0
	replace medicare50 					= 1 ///
										if medicare == 1

	// Interact medicare dummy with post_qtr and after_MN
	gen 	after_MN_medicare 			= after_MN * medicare
	gen 	post_qtr_medicare 			= post_qtr * medicare
	gen 	after_MN_post_qtr_medicare 	= post_qtr_after_MN * medicare

	// Interact traditional medicare dummy with post_qtr and after_MN
	gen 	after_MN_tradmedicare 			= after_MN * trad_medicare
	gen 	post_qtr_tradmedicare 			= post_qtr * trad_medicare
	gen 	after_MN_post_qtr_tradmedicare 	= post_qtr_after_MN * trad_medicare

	//Interact medicare dummy with post_qtr and after_MN
	gen 	after_MN_medicare50 		= after_MN * medicare50
	gen 	post_qtr_medicare50 		= post_qtr * medicare50
	gen 	after_MN_post_qtr_medicare50 = post_qtr_after_MN * medicare50

	// Convert variables from string to number so they can be used as a factor variable
	encode dshospid, 		gen(dshospid2)
	encode pointoforigin_x, gen(pointoforigin2)
	encode race_x, 			gen(race2)
	encode hispanic_x, 		gen(hispanic2)

	// Generate age bins
	egen agebin = cut(age), at(5,10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125)

	gen 	white = race_x == "5"
	gen  	hispanic = hispanic_x == "E1"

	replace chg_ED = 0 if missing(chg_ED)


gen     out_notobs                  = cond(hcup_os == 0 & inpatient == 0, 1, 0)
gen     in_obs                      = cond(chg_obs_d == 1 & inpatient == 1, 1, 0)
gen     in_notobs                   = cond(chg_obs_d == 0 & inpatient == 1, 1, 0) 
save "${SIDDataPath}/sidsedd_merged_20102016.dta", replace

log close

